﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;

namespace WebSEClassLibrary
{
    public class ActPage
    {

        private string picURL;          //图片URL
        private string chatContent;     //对话内容
        private bool isChatPage;        //是否对话页面
        private string userID;          //用户id***********************************************新增
        private int pageID;             //页面id***********************************************新增

        private int proID;              //题目id
        private int problemType;        //题目类型，1为单选题，2为多选题，3为填空题
        private string problem;         //题目

        private string[] choices;       //选择题选项，固定4个
        private int countChsAns;        //选择题答案个数
        private int[] chsAns;           //选择题答案，0、1、2和3表示选A、B、C和D
        private string[] explainChs;    //选择题答案解释，每个选项都要给出解释
        private int[] userChsAns;       //用户答案***********************************************新增

        private int countFillAns;       //填空题空格个数
        private int[] countSimilar;     //每个空的答案数
        private string[][] fillAns;     //填空题答案，每个空可能多个类似的答案
        private string explainFill;     //填空题答案解释
        private string[] userFillAns;   //用户答案***********************************************新增


        private string connStr = @"Data Source=localhost; Initial Catalog=WebSEData; uid=sa;Password=kkkkkk";
        private SqlConnection connection;
        private SqlCommand cmd;
        private SqlDataReader reader;

        public string[][] FillAns
        {
            get
            {
                return fillAns;
            }
            private set
            {
                fillAns = value;
            }
        }

        public int[] UserChsAns
        {
            get
            {
                return userChsAns;
            }
            set
            {
                userChsAns = value;
            }
        }

        public int PageID
        {
            get
            {
                return pageID;
            }
            private set
            {
                pageID = value;
            }
        }

        public String UserID
        {
            get
            {
                return userID;
            }
            set
            {
                userID = value;
            }
        }

        //构造函数
        public ActPage(int pageID)
        {
            PageID = pageID;

            connection = new SqlConnection(connStr);
            cmd = connection.CreateCommand();

            setPage();
        }

        public string PicURL
        {
            get
            {
                return picURL;
            }
            private set
            {
                picURL = value;
            }
        }

        public string ChatContent
        {
            get
            {
                return chatContent;
            }
            private set
            {
                chatContent = value;
            }
        }

        public bool IsChatPage
        {
            get
            {
                if (problemType == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public int ProID
        {
            get
            {
                return proID;
            }
            private set
            {
                proID = value;
            }
        }

        public int ProblemType
        {
            get
            {
                return problemType;
            }
            private set
            {
                problemType = value;
            }
        }

        public string Problem
        {
            get
            {
                return problem;
            }
            private set
            {
                problem = value;
            }
        }

        public string[] Choices
        {
            get
            {
                return choices;
            }
            private set
            {
                choices = value;
            }
        }

        public int CountChsAns
        {
            get
            {
                return ChsAns.Length;
            }
        }

        public int[] ChsAns
        {
            get
            {
                return chsAns;
            }
            private set
            {
                chsAns = value;
            }
        }

        public string[] ExplainChs
        {
            get
            {
                return explainChs;
            }
            private set
            {
                explainChs = value;
            }
        }

        public int CountFillAns
        {
            get
            {
                return countFillAns;
            }
            private set
            {
                countFillAns = value;
            }
        }

        public int[] CountSimilar
        {
            get
            {
                return countSimilar;
            }
        }

        public string ExplainFill
        {
            get
            {
                return explainFill;
            }
            set
            {
                explainFill = value;
            }
        }

        public string[] UserFillAns
        {
            get
            {
                return userFillAns;
            }
            set
            {
                userFillAns = value;
            }
        }

        //设置页面对象的各个属性
        private void setPage()
        {
            try
            {
                if (connection.State==ConnectionState.Open)
                {
                    connection.Close();
                }
                connection.Open();
                cmd.CommandText = "select Image,Words,QType,QID from Page where ID = '" + PageID.ToString() + "'";
                reader = cmd.ExecuteReader();
                reader.Read();
                PicURL = reader.GetString(0);
                ChatContent = reader.GetString(1);
                ProblemType = int.Parse(reader.GetValue(2).ToString());
                if (ProblemType != 0)
                {
                    ProID = int.Parse(reader.GetString(3));
                    getProblem();
                    if (UserID!=null)
                    {
                        getProblemAnswer();
                    }                    
                }
                reader.Close();
                connection.Close();
            }
            catch (System.Exception e)
            {
                connection.Close();
                problem = e.Message;
            }
        }

        private void getProblem()
        {
            try
            {
                if (connection.State==ConnectionState.Open)
                {
                    connection.Close();
                }
                connection.Open();
                if (ProblemType == 1 || ProblemType == 2)
                {
                    if (!reader.IsClosed)
                    {
                        reader.Close();
                    }
                    cmd.CommandText = "select Title,Choices,Answers,Explanation from ChoiceQuestion where ID='" + ProID + "'";
                    reader = cmd.ExecuteReader();
                    reader.Read();
                    Problem = reader.GetString(0);

                    choices[0] += "reader";

                    Choices = reader.GetString(1).Split(new char[] { '/' });
                    String[] token = reader.GetString(2).Split(new char[] { '/' });
                    chsAns = new int[token.Length];
                    for (int i = 0; i < token.Length; i++)
                    {
                        chsAns[i] = int.Parse(token[i]);
                    }
                    ExplainChs = reader.GetString(3).Split(new char[] { '/' });
                    reader.Close();
                }
                if (ProblemType == 3)
                {
                    cmd.CommandText = "select Title,Answers,Explanation from GapFillQuestion where ID='" + ProID + "'";
                    reader = cmd.ExecuteReader();
                    reader.Read();
                    Problem = reader.GetString(0);

                    String[] token = reader.GetString(1).Split(new char[] { '/' });
                    CountFillAns = token.Length;
                    countSimilar = new int[CountFillAns];
                    fillAns = new string[CountFillAns][];
                    for (int i = 0; i < CountFillAns; i++)
                    {
                        fillAns[i] = token[i].Split(new char[] { '$' });
                        countSimilar[i] = fillAns[i].Length;
                    }

                    ExplainFill = reader.GetString(2);

                    reader.Close();
                }
                connection.Close();
            }
            catch (System.Exception e)
            {
                connection.Close();
                choices = new string[4];
                choices[0] = e.Message;
            }
        }

        private void getProblemAnswer()
        {
            try
            {
                if (connection.State==ConnectionState.Open)
                {
                    connection.Close();
                }
                connection.Open();
                if (ProblemType == 1 || ProblemType == 2)
                {
                    cmd.CommandText = "select Answers from ChoiceReport where QID='" + ProID + "' and UID = '" + UserID + "'";
                    reader = cmd.ExecuteReader();
                    reader.Read();
                    string[] token = reader.GetString(0).Split(new char[] { '|' });
                    userChsAns = new int[token.Length];
                    for (int i = 0; i < token.Length; i++)
                    {
                        userChsAns[i] = int.Parse(token[i]);
                    }
                    reader.Close();
                }
                if (ProblemType == 3)
                {
                    cmd.CommandText = "select Answers from GapFillReport where QID='" + ProID + "' and UID = '" + UserID + "'";
                    reader.Read();
                    UserFillAns = reader.GetString(0).Split(new char[] { '|' });
                    reader.Close();
                }
                connection.Close();
            }
            catch (System.Exception e)
            {
                connection.Close();
                problem += e.Message;
                problem += "<br/>";
            }
        }

        public void writeDataBase()
        {
            try
            {
                if (connection.State==ConnectionState.Open)
                {
                    connection.Close();
                }
                connection.Open();
                if (ProblemType==1 || ProblemType==2)
                {
                    String ans = "";
                    for (int i = 0; i < UserChsAns.Length - 1; i++)
                    {
                        ans += UserChsAns[i]+"/";
                    }
                    ans += UserChsAns[UserChsAns.Length - 1];
                    cmd.CommandText = "Insert into ChoiceReport values('" + ProID + "','" + UserID + "','" + ans + "')";
                    cmd.ExecuteNonQuery();
                }
                if (ProblemType==3)
                {
                    String ans = "";
                    for (int i = 0; i < UserFillAns.Length - 1; i++)
                    {
                        ans += UserFillAns[i];
                    }
                    cmd.CommandText = "Insert into GapFillReport values('" + ProID + "','" + UserID + "','" + ans + "')";
                    cmd.ExecuteNonQuery();
                }
                connection.Close();
            }
            catch (System.Exception e)
            {
                connection.Close();
                problem += e.Message;
                problem += "<br/>";
            }
        }
    }

}
